package controller;

import helper.UserSession;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import databeans.Customer;
import databeans.Employee;


import model.CustomerDAO;
import model.Model;


public class CustomerLoginAction extends Action {
    private CustomerDAO customerDAO;

    public CustomerLoginAction(Model model) {
        customerDAO = model.getCustomerDAO();
    }

    public String getName() {
        return "customerHome.do";
    }

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
    	String loginPage = "customer/customer-login.jsp";
        String dashboardPage = "customerDashboard.do";
        String originPage = loginPage;
        String successPage = "customerDashboard.do";
        
        //set the employee
        Employee employee = null;
        Customer customer = null;

        
        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        
        // Define user type for this controller
        request.setAttribute("page", "cus-login");
        
        //get all the parameter from the form
        String requestType = request.getParameter("Submit");
        String username = request.getParameter("txtUsername");
        String password = request.getParameter("txtPassword");
        
        //reload the form field
        request.setAttribute("txtUsername", username);
        request.setAttribute("txtPassword", password);
        
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
        employee = (Employee) request.getSession(true).getAttribute("employee");
        if (employee != null) {
            request = UserSession.removeEmployeeFromSession(request);
            errors.add("This section is closed for employee");
            return originPage ;
        }
        
        //check if customer has actually logged in
        customer = (Customer) request.getSession(true).getAttribute("customer");
        if (customer != null) {
            request.setAttribute("page", "cust-home");
            request.setAttribute("user-type", "customer");
            return successPage;

        }
        
        //validate if user has necessary qualification
        //check the session

        // check for get or post request
        //if there is no parameter get passed, return to origin page
        if (requestType == null) {
            return originPage;     
        }
        
        //validate the input
        if (username.trim().equals("")) {
            errors.add("Username cannot be empty. Please check.");
            return originPage;
        }
        
        if (password.trim().equals("")){
        	errors.add("Password cannot be empty. Please check.");
            return originPage;
        }
        

        // at this stage, all validation should have been done
        try {
            //get the list of all customer         
            customer = customerDAO.lookup(username);
            
            if (customer == null) {
                errors.add("Incorrect username or password. Please check.");
                return originPage;
            }

            // Check the password

            if (!customer.checkPassword(password)) {
            	errors.add("Incorrect username or password. Please check.");
                return originPage;
            }
            
            // Attach (this copy of) the employee bean to the session
            HttpSession session = request.getSession(true); 
            session.setAttribute("customer", customer);
            request.setAttribute("page", "cust-home");
            
            return "customerDashboard.do";

        } catch (Exception e) {
            errors.add("Unable to validate email and password.");
            System.out.println(e.getClass() + " " + e.getMessage() + " / " + e.getLocalizedMessage());
            return originPage;
        }

    }
}